#include <stdio.h> 
#include <stdlib.h>
#include <unistd.h>

int g_val = 100;
int main()
{
  pid_t id = fork();
  int flag = 0; 
  if(id == 0)
  {
    //child
    while(1)
    {
      printf("我是子进程：%d,ppid：%d,g_val:%d,&g_val:%p\n\n",getpid(),getppid(),g_val,&g_val);
      sleep(1);
      flag++;
      if(flag == 5)
      {
        g_val = 200;
        printf("我是子进程，全局数据我已经修改了，用户请注意查看\n");
      }
    }
  }
  else
  {
    //parent 
    while(1)
    {
      printf("我是父进程：%d,ppid：%d,g_val:%d,&g_val:%p\n\n",getpid(),getppid(),g_val,&g_val);
      sleep(1); 
    }
  }
  return 0;
}






/*
int un_g_val;//未初始化全局变量
int g_val  =100; //初始化全局变量
int main(int argc,char *argv[],char* env[])
{
  printf("code addr               : %p\n",main);//函数
  printf("init global addr        : %p\n",&g_val);//初始化全局变量
  printf("uninit global addr      : %p\n",&un_g_val);//未初始化全局变量
  char *m1 = (char*)malloc(100);
  char *m2 = (char*)malloc(100);
  char *m3 = (char*)malloc(100);
  char *m4 = (char*)malloc(100);
  static int s = 100;
  printf("heap addr               : %p\n",m1);//堆
  printf("heap addr               : %p\n",m2);//堆
  printf("heap addr               : %p\n",m3);//堆
  printf("heap addr               : %p\n",m4);//堆

  
  printf("stack addr               : %p\n",&m1);//栈
  printf("stack addr               : %p\n",&m2);//栈
  printf("stack addr               : %p\n",&m3);//栈
  printf("stack addr               : %p\n",&m4);//栈

  printf("s static addr            : %p\n",&s);//静态变量
  for(int i  =0;i<argc;i++)
  {
    printf("argv addr              : %p\n",argv[i]);
  }

  for(int i = 0;env[i];i++)
  {
    printf("env addr               : %p\n",env[i]);
  }

  return 0;
}
*/
